package com.rivulet.foreign.config;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * {shiro过滤器}
 *
 * @author xujiac
 * @create 2021/7/19
 * @since 1.0.0
 */
@Configuration
public class ShiroConfig {
    @Bean
    public Realm getRealm() {
        LoginRealm realm = new LoginRealm();
        return realm;
    }

    @Bean
    public SecurityManager getSecurityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(getRealm());
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean setShiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/login", "anon");
        map.put("/img/**", "anon");
        map.put("/js/**", "anon");
        map.put("/css/**", "anon");
        map.put("/logout", "logout");
        map.put("/**", "authc");
        factoryBean.setLoginUrl("/");
        factoryBean.setFilterChainDefinitionMap(map);
        return factoryBean;
    }
}
